package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageInFoDomain;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.ElderlyFallRecord;
import com.ruoyi.system.domain.FailMsg;
import com.ruoyi.system.domain.Info;
import com.ruoyi.system.service.IElderlyFallRecordService;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @author jinyao
 */
@RestController
@RequestMapping("/system/elderlyFallRecord")
@RequiredArgsConstructor
public class ElderlyFallRecordController {

    private final IElderlyFallRecordService elderlyFallRecordService;

    private final RedisCache redisCache;

    @PostMapping("/list")
    public AjaxResult list(ElderlyFallRecord record) {
        PageInFoDomain<ElderlyFallRecord> page = elderlyFallRecordService.list(record);
        return AjaxResult.success(page);
    }

    @Anonymous
    @GetMapping("/showfail")
    public AjaxResult showfail() {
        FailMsg msg = new FailMsg();
        Map<String, String> fail = redisCache.getCacheMap("fail");
        if(!fail.isEmpty()){
            List<Info> infos = setInfos(msg, fail);
            msg.setInfos(infos);
            return AjaxResult.success(msg);
       }
       msg.setCode(1);
       msg.setMessage("暂无摔倒记录");
       return AjaxResult.success(msg);
    }

    @Anonymous
    @GetMapping("/roger/{key}")
    public AjaxResult roger(@PathVariable("key") String key) {
        Map<String, String> fail = redisCache.getCacheMap("fail");
        boolean b = false;
        if (!fail.isEmpty()){
            b = redisCache.deleteCacheMapValue("fail", key);
        }
        return b ? AjaxResult.success() : AjaxResult.error();
    }

    private  @NotNull List<Info> setInfos(FailMsg msg, Map<String, String> fail) {
        msg.setCode(0);
        msg.setMessage("有人摔到了");
        List<Info> infos = new ArrayList<>();
        fail.forEach((k, v)->{
            Info info = new Info();
            info.setKey(k);
            info.setValue(v);
            infos.add(info);
        });
        return infos;
    }

    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ElderlyFallRecord record =   elderlyFallRecordService.selectById(id);
        return AjaxResult.success(record);
    }

    @Anonymous
    @PostMapping("/fall")
    public AjaxResult fall(@RequestBody ElderlyFallRecord record) {
        int insert = elderlyFallRecordService.insert(record);
        CompletableFuture.runAsync(() ->
              redisCache.setCacheMapValue("fail",record.getRecordId().toString(),record.getImageUrls())
        );
        return AjaxResult.success(insert);
    }

    @Anonymous
    @PostMapping("/end")
    public AjaxResult end(@RequestBody ElderlyFallRecord record){
        int end = elderlyFallRecordService.end(record);
        return AjaxResult.success(end);
    }


}
